using System;
using LocalOverride.Fiddler.UrlMapping;
using NSubstitute;
using NUnit.Framework;

namespace LocalOverride.Tests.UrlMapping
{
    [TestFixture]
    public class MappingEntryQueryStringIncludedTests
    {
        [Test]
        public void WillRequestSpecialFileName_WhenExtensionIsOnQueryStringIncludedList()
        {
            var uniquePathProvider = Substitute.For<ISpecialCaseMappingsProvider>();
            uniquePathProvider.GetMappingResult(null).ReturnsForAnyArgs(MappingResult.NoMapping);

            string requestUrl = "http://www.test.com/webresource.axd?id=3423";

            string specialCaseResult = @"special case result";
            string genericCaseResult = @"c:\temp\webresource.axd";

            uniquePathProvider.MakeSpecialCase(requestUrl, genericCaseResult).Returns(specialCaseResult);

            // Arrange
            var mapping = new MappingRegistry();
            mapping.AddMapping(
                new DirectoryMapping(
                    new Uri("http://www.test.com/")
                    , @"c:\temp", new string[0], true, null, new[]{"axd"}, uniquePathProvider)
                );

            // Act
            
            var requestUri = new Uri(requestUrl);
            var result = mapping.GetMapping(requestUri);

            // Assert
            Assert.AreEqual(specialCaseResult, result.LocalFile);
        }

        [Test]
        public void WillUseSpecialCaseWhereAvailable()
        {
            string requestUrl = "http://www.test.com/webresource.axd?id=3423";

            var uniquePathProvider = Substitute.For<ISpecialCaseMappingsProvider>();
            string specialCaseResult = @"c:\dafasdfasdf.asdfasdf";

            uniquePathProvider.GetMappingResult(requestUrl).Returns(new MappingResult(specialCaseResult, true));
            
            // Arrange
            var mapping = new MappingRegistry();
            mapping.AddMapping(
                new DirectoryMapping(
                    new Uri("http://www.test.com/")
                    , @"c:\temp", new string[0], true, null, new[] { "axd" }, uniquePathProvider)
                );

            // Act
            
            var requestUri = new Uri(requestUrl);
            var result = mapping.GetMapping(requestUri);

            // Assert
            Assert.AreEqual(specialCaseResult, result.LocalFile);
        }

        [Test]
        public void WillDownloadMissingFile_WhereEntrySaysDownload()
        {
            string requestUrl = "http://www.test.com/webresource.axd?id=3423";

            var uniquePathProvider = Substitute.For<ISpecialCaseMappingsProvider>();
            string specialCaseResult = @"c:\dafasdfasdf.asdfasdf";

            uniquePathProvider.GetMappingResult(requestUrl).Returns(new MappingResult(specialCaseResult, true));

            // Arrange
            var mapping = new MappingRegistry();
            mapping.AddMapping(
                new DirectoryMapping(
                    new Uri("http://www.test.com/")
                    , @"c:\temp", new string[0], true, null, new[] { "axd" }, uniquePathProvider)
                );

            // Act

            var requestUri = new Uri(requestUrl);
            var result = mapping.GetMapping(requestUri);

            // Assert
            Assert.IsTrue(result.DownloadMissingFile);
        }

        [Test]
        public void WillNotDownloadMissingFile_WhereEntrySaysDoNotDownload()
        {
            string requestUrl = "http://www.test.com/webresource.axd?id=3423";

            var uniquePathProvider = Substitute.For<ISpecialCaseMappingsProvider>();
            string specialCaseResult = @"c:\dafasdfasdf.asdfasdf";

            uniquePathProvider.GetMappingResult(requestUrl).Returns(new MappingResult(specialCaseResult, true));

            // Arrange
            var mapping = new MappingRegistry();
            mapping.AddMapping(
                new DirectoryMapping(
                    new Uri("http://www.test.com/")
                    , @"c:\temp", new string[0], false, null, new[] { "axd" }, uniquePathProvider)
                );

            // Act

            var requestUri = new Uri(requestUrl);
            var result = mapping.GetMapping(requestUri);

            // Assert
            Assert.IsFalse(result.DownloadMissingFile);
        }
    }
}